package com.sqlcheck.controller;

import com.sqlcheck.dto.request.ManualSqlAnalysisRequest;
import com.sqlcheck.dto.response.ManualSqlAnalysisResponse;
import com.sqlcheck.entity.ManualSqlAnalysis;
import com.sqlcheck.service.ManualSqlAnalysisService;
import com.sqlcheck.common.response.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

@Slf4j
@RestController
@RequestMapping("/manual-sql")
@RequiredArgsConstructor
@Validated
@Tag(name = "手动SQL分析", description = "手动SQL兼容性分析相关接口")
@Deprecated // 暂时关闭SQL在线分析功能
public class ManualSqlAnalysisController {

    private final ManualSqlAnalysisService manualSqlAnalysisService;

    @PostMapping("/analyze")
    @Operation(summary = "分析SQL兼容性", description = "对用户输入的SQL进行兼容性分析，支持实时分析")
    @ApiResponses(value = {
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "分析成功", content = @Content(schema = @Schema(implementation = ManualSqlAnalysisResponse.class))),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "请求参数错误", content = @Content(schema = @Schema(implementation = ApiResponse.class))),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误", content = @Content(schema = @Schema(implementation = ApiResponse.class)))
    })
    @Deprecated // 暂时关闭
    public ResponseEntity<ApiResponse<ManualSqlAnalysisResponse>> analyzeSql(
            @Valid @RequestBody @Parameter(description = "SQL分析请求") ManualSqlAnalysisRequest request) {
        
        // 暂时关闭功能，返回维护中信息
        return ResponseEntity.ok(ApiResponse.error(
            "503", 
            "SQL在线分析功能暂时关闭维护中，请稍后再试"
        ));

        /* 原有实现已暂时注释
        try {
            log.info("收到手动SQL分析请求，源数据库: {}, 目标数据库: {}",
                    request.getSourceDbType(), request.getTargetDbType());

            ManualSqlAnalysisResponse result = manualSqlAnalysisService.analyzeSql(request);

            if (result.isSuccess()) {
                return ResponseEntity.ok(ApiResponse.success(result));
            } else {
                return ResponseEntity.badRequest()
                        .body(ApiResponse.error(result.getErrorMessage()));
            }

        } catch (Exception e) {
            log.error("手动SQL分析失败", e);
            return ResponseEntity.internalServerError()
                    .body(ApiResponse.error("分析过程中发生错误: " + e.getMessage()));
        }
        */
    }

    @PostMapping("/save")
    @Operation(summary = "保存分析任务", description = "保存SQL分析任务到数据库")
    @ApiResponses(value = {
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "保存成功", content = @Content(schema = @Schema(implementation = Long.class))),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "400", description = "请求参数错误", content = @Content(schema = @Schema(implementation = ApiResponse.class))),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "500", description = "服务器内部错误", content = @Content(schema = @Schema(implementation = ApiResponse.class)))
    })
    @Deprecated // 暂时关闭
    public ResponseEntity<ApiResponse<Long>> saveAnalysisTask(
            @Valid @RequestBody @Parameter(description = "SQL分析请求") ManualSqlAnalysisRequest request) {

        // 暂时关闭功能
        return ResponseEntity.ok(ApiResponse.error(
            "503", 
            "SQL在线分析功能暂时关闭维护中，请稍后再试"
        ));

        /* 原有实现已暂时注释
        try {
            log.info("保存手动SQL分析任务");

            Long taskId = manualSqlAnalysisService.saveAnalysisTask(request);

            return ResponseEntity.ok(ApiResponse.success(taskId));

        } catch (Exception e) {
            log.error("保存手动SQL分析任务失败", e);
            return ResponseEntity.internalServerError()
                    .body(ApiResponse.error("保存任务失败: " + e.getMessage()));
        }
        */
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取分析任务", description = "根据ID获取SQL分析任务详情")
    @ApiResponses(value = {
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "获取成功", content = @Content(schema = @Schema(implementation = ManualSqlAnalysis.class))),
            @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "404", description = "任务不存在", content = @Content(schema = @Schema(implementation = ApiResponse.class)))
    })
    @Deprecated // 暂时关闭
    public ResponseEntity<ApiResponse<ManualSqlAnalysis>> getAnalysisTask(
            @PathVariable @Parameter(description = "任务ID") Long id) {

        // 暂时关闭功能
        return ResponseEntity.ok(ApiResponse.error(
            "503", 
            "SQL在线分析功能暂时关闭维护中，请稍后再试"
        ));

        /* 原有实现已暂时注释
        try {
            ManualSqlAnalysis task = manualSqlAnalysisService.getAnalysisTask(id);

            if (task != null) {
                return ResponseEntity.ok(ApiResponse.success(task));
            } else {
                return ResponseEntity.notFound().build();
            }

        } catch (Exception e) {
            log.error("获取手动SQL分析任务失败, ID: {}", id, e);
            return ResponseEntity.internalServerError()
                    .body(ApiResponse.error("获取任务失败: " + e.getMessage()));
        }
        */
    }
}